package com.tanhua.server.controller;

import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 下午 13:32 2021/1/8
 * @ModifiedBy:
 */
@RestController
@RequestMapping("comments")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;

    /**
       * @Description: 查看评论列表
       * @Param:[publishId, page, pageSize]
       * @return:org.springframework.http.ResponseEntity<com.tanhua.server.vo.PageResult>
       * @Author:JinboYang
       * @Date:2021/1/8
       */
    @GetMapping
    public ResponseEntity<PageResult> queryCommentsList(@RequestParam("movementId") String publishId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        try {
            PageResult pageResult = this.commentsService.queryCommentsList(publishId, page, pageSize);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
       * @Description: 发表评论
       * @Param:[param]
       * @return:org.springframework.http.ResponseEntity<java.lang.Void>
       * @Author:JinboYang
       * @Date:2021/1/8
       */
    @PostMapping
    public ResponseEntity<Void> saveComments(@RequestBody Map<String,String> param){
        try {
            String publishId = param.get("movementId");
            String comment = param.get("comment");
            Boolean bool = this.commentsService.saveComments(publishId,comment);
            if(bool){
                //发送消息
                this.quanziMQService.commentPublishMsg(publishId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
       * @Description: 点赞
       * @Param:[commentId]
       * @return:org.springframework.http.ResponseEntity<java.lang.Long>
       * @Author:JinboYang
       * @Date:2021/1/8
       */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String commentId) {
        try {
            Long count = this.movementsService.likeComment(commentId);
            if (null != count) {
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
       * @Description: 取消点赞
       * @Param:[commentId]
       * @return:org.springframework.http.ResponseEntity<java.lang.Long>
       * @Author:JinboYang
       * @Date:2021/1/8
       */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String commentId) {
        try {
            Long count = this.movementsService.disLikeComment(commentId);
            if (null != count) {
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
